<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>MutationObserver | 夏栀璃鸢浅汐与潮</title>
    <meta name="generator" content="VuePress 1.5.2">
    <link rel="shortcut icon" href="/favicon.ico">
    <meta name="description" content="简简单单即是真">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/assets/css/0.styles.94c5e4fc.css" as="style"><link rel="preload" href="/assets/js/app.3219c992.js" as="script"><link rel="preload" href="/assets/js/3.95fbbe62.js" as="script"><link rel="preload" href="/assets/js/1.87858ac8.js" as="script"><link rel="preload" href="/assets/js/10.4bcbd4ad.js" as="script"><link rel="prefetch" href="/assets/js/11.0a93cac4.js"><link rel="prefetch" href="/assets/js/12.cc88334e.js"><link rel="prefetch" href="/assets/js/4.e3c180dd.js"><link rel="prefetch" href="/assets/js/5.9adc7ef1.js"><link rel="prefetch" href="/assets/js/6.4e8c6b5f.js"><link rel="prefetch" href="/assets/js/7.479503fb.js"><link rel="prefetch" href="/assets/js/8.00caac66.js"><link rel="prefetch" href="/assets/js/9.ad1151a9.js">
    <link rel="stylesheet" href="/assets/css/0.styles.94c5e4fc.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-dad8a512><div data-v-dad8a512><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-dad8a512 data-v-dad8a512><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-dad8a512 data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>夏栀璃鸢浅汐与潮</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>瓜子果子栗子</span>
            
          <!---->
          2020
        </a></span></div></div> <div class="hide" data-v-dad8a512><header class="navbar" data-v-dad8a512><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.jpg" alt="夏栀璃鸢浅汐与潮" class="logo"> <span class="site-name">夏栀璃鸢浅汐与潮</span></a> <div class="links"><!----> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-dad8a512></div> <aside class="sidebar" data-v-dad8a512><div class="personal-info-wrapper" data-v-ca798c94 data-v-dad8a512><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    瓜子果子栗子
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>2</h3> <h6 data-v-ca798c94>Article</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>2</h3> <h6 data-v-ca798c94>Tag</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>MutationObserver</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>瓜子果子栗子</span>
            
          <!---->
          2020
        </a></span></div></div> <div data-v-dad8a512><main class="page"><div class="page-title" style="display:none;"><h1>MutationObserver</h1> <hr> <div data-v-aa64b9d0><i class="iconfont reco-account" data-v-aa64b9d0><span data-v-aa64b9d0>瓜子果子栗子</span></i> <i class="iconfont reco-date" data-v-aa64b9d0><span data-v-aa64b9d0>2020-07-15</span></i> <!----> <i class="iconfont reco-tag tags" data-v-aa64b9d0><span class="tag-item" data-v-aa64b9d0>Web API</span><span class="tag-item" data-v-aa64b9d0>笔记</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><div class="custom-block tip"><p>MutationObserver接口提供了监视DOM树所做的更改的能力。
他被设计为旧MutationEvents功能的替代品，该功能是DOM3 Events规范的一部分</p></div> <h2 id="构造函数"><a href="#构造函数" class="header-anchor">#</a> 构造函数</h2> <p>MutationObserver(callback)</p> <details class="custom-block details"><summary>详细信息</summary> <p>该构造函数会创建并返回一个新的观察器，它会在触发指定DOM事件时，调用指定的callback。MutationObserver对DOM的观察不会立即启动；必须调用<a href="#observer-targer-options">observe()</a>方法来确定要监听哪一部分的DOM以及要响应哪些变更。</p></details> <h2 id="语法"><a href="#语法" class="header-anchor">#</a> 语法</h2> <div class="custom-block tip"><div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> observer<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MutationObserver</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span>
</code></pre></div><ul><li>参数
<ul><li><em><strong>callback</strong></em> <ul><li>一个回调函数，每当被指定的DOM节点或子树以及配置项有变动时会被调用。回调函数拥有两个参数：一个是描述所有被触发改动的<a href="/blog/笔记/MutationRecord.html">MutationRecord</a>对象数组，另一个是调用该函数的<a href="#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0">MutationOserver</a>对象</li></ul></li> <li><em><strong>返回值</strong></em> <ul><li>一个新的、包含监听DOM变化回调函数的<strong>MutationObserver</strong>对象</li></ul></li></ul></li></ul></div> <h2 id="方法"><a href="#方法" class="header-anchor">#</a> 方法</h2> <h3 id="observer-targer-options"><a href="#observer-targer-options" class="header-anchor">#</a> observer(targer[, options])</h3> <div class="custom-block tip"><p>该方法用来开始监听并根据设置要监听的DOM和配置</p></div> <ul><li>参数
<ul><li>targer
<ul><li>要监听的DOM，或者是要监听的节点树的根节点</li></ul></li> <li>options(<em>可选</em> )
<ul><li>此对象用来配置DOM的哪些变化要被监听</li></ul></li></ul></li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code>options<span class="token operator">:</span><span class="token punctuation">{</span>
    <span class="token comment">//要监听的特点属性名称的数组。如果没有指定就会对所有属性更改进行监听</span>
    attributeFilter<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">string</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token comment">//当监听属性变更时，将此属性设置为true将记录改动的属性的上一个值</span>
    attributeOldValue<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">boolean</span>
    <span class="token comment">//设为true将监听属性的变更.默认值为false</span>
    attributes<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">boolean</span>
    <span class="token comment">//设为true监听字符数据的变更</span>
    characterData<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">boolean</span>
    <span class="token comment">//效果同attributeOldValue</span>
    characterDataOldValue<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">boolean</span>
    <span class="token comment">//设为true监听目标节点添加或删除子节点。默认值为false</span>
    childList<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">boolean</span>
    <span class="token comment">//设为true将监听范围扩大到目标节点的所有节点</span>
    subtree<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">boolean</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>异常
<ul><li>配置的选项没有建设任何内容时会抛出异常(例如：childList,attributes,charaterData都为false)</li> <li>attributes为false但是attributeOldValue为true或者attributeFilter配置存在</li> <li>characterDataOldValue为true但是characterData为false</li></ul></li></ul> <div class="custom-block tip"><p>可以在同一个MutationObserver对象多次调用observe()方法，来监听DOM树中不同的部分的变化</p> <div class="custom-block warning"><p>如果调用多次observe()来监听同一个DOM，则在新的监听器生效前，现有的监听器会被移除</p></div></div> <div class="custom-block tip"><p>如果监听的节点的子树且这个子树的一部分被移动到DOM中的其他位置，被分离的节点依然会被监听，会触发与分离之前相同的回调</p></div> <h3 id="takerecords"><a href="#takerecords" class="header-anchor">#</a> takeRecords()</h3> <div class="custom-block tip"><p>该方法返回已检测到但尚未由监听的回调函数处理的所有匹配DOM更改的列表</p></div> <ul><li>返回值
<ul><li>返回一个<a href="">MutationRecord</a>对象列表，每个对象都描述了DOM树某部分的一次变动</li></ul></li></ul> <div class="custom-block warning"><p>调用tackRecords()后，已发生但未传递给回调的变更队列将被清空，也就不会触发回调函数了</p></div> <h3 id="disconnect"><a href="#disconnect" class="header-anchor">#</a> disconnect()</h3> <div class="custom-block tip"><p>调用该方法会停止监听。</p> <p>如果被监听的元素从DOM中移除，返回被垃圾回收释放，这个MutationObserver将同样被删除</p></div> <div class="custom-block warning"><p>所以已检测到但是尚未传递给监听器的变动都将被丢弃</p></div> <h2 id="示例"><a href="#示例" class="header-anchor">#</a> 示例</h2> <p>演示了创建一个新的<strong>MutationObserver</strong>,监听一个节点及全部子节点的添加，移除元素，以及属性变化的事件</p> <ul><li><strong>回调函数</strong></li></ul> <div class="custom-block tip"><div class="language-js extra-class"><pre class="language-js"><code>        <span class="token comment">/*
        mutationRecords是一个数组，里面存放着mutationRecord实例
        每一个实例都代表一个独立的DOM变化
        */</span>
        <span class="token keyword">function</span> <span class="token function">callback</span><span class="token punctuation">(</span><span class="token parameter">mutationRecords<span class="token punctuation">,</span>observer</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            mutationRecords<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">mutationRecord</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>mutationRecord<span class="token punctuation">.</span>type<span class="token operator">===</span><span class="token string">'childList'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'从树上添加或移除一个或更多的子节点'</span><span class="token punctuation">)</span>
                <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>mutationRecord<span class="token punctuation">.</span>type<span class="token operator">===</span><span class="token string">'attributes'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'一个属性值被更改'</span><span class="token punctuation">)</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
</code></pre></div></div> <p>调用<a href="#observer-targer-options">observe()</a>即可开始监听DOM。当observer发现匹配监听请求中指定的配置项的更改时,<strong>callback</strong>方法便会被调用</p> <p>使用<strong>mutationRecord.type</strong>获取发生的变动类型</p> <ul><li><strong>创建并使用observer</strong></li></ul> <div class="custom-block tip"><div class="language-js extra-class"><pre class="language-js"><code>        <span class="token keyword">let</span> observer<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MutationObserver</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span>

        <span class="token keyword">const</span> box <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.box'</span><span class="token punctuation">)</span>
        <span class="token keyword">const</span> observerOptions<span class="token operator">=</span><span class="token punctuation">{</span>
            childList<span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span><span class="token comment">//监听目标子节点的变化，是否有添加或删除</span>
            attributes<span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span><span class="token comment">//监听属性变动</span>
            subtree<span class="token operator">:</span><span class="token boolean">true</span><span class="token comment">//监听后代节点，默认位false</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//开始监听</span>
        observer<span class="token punctuation">.</span><span class="token function">observe</span><span class="token punctuation">(</span>box<span class="token punctuation">,</span>observerOptions<span class="token punctuation">)</span>
</code></pre></div></div> <p>使用类名获取到目标节点。<strong>observerOptions</strong>中设定了监听的选项,通过设定<strong>childList</strong>和<strong>attributes</strong>为<strong>true</strong>来获取信息</p> <p>当调用<strong>observer</strong>方法时，就会根据配置的选项开始监听指定的DOM节点</p> <p>直到调用<a href="#disconnect">disconnect()</a>,每次为指定DOM节点添加或移除元素，以及属性进行变更时，<strong>callback</strong>都会被调用</p></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-40a3448a><li class="level-2" data-v-40a3448a><a href="/blog/%E7%AC%94%E8%AE%B0/MutationObserver.html#构造函数" class="sidebar-link" data-v-40a3448a>构造函数</a></li><li class="level-2" data-v-40a3448a><a href="/blog/%E7%AC%94%E8%AE%B0/MutationObserver.html#语法" class="sidebar-link" data-v-40a3448a>语法</a></li><li class="level-2" data-v-40a3448a><a href="/blog/%E7%AC%94%E8%AE%B0/MutationObserver.html#方法" class="sidebar-link" data-v-40a3448a>方法</a></li><li class="level-3" data-v-40a3448a><a href="/blog/%E7%AC%94%E8%AE%B0/MutationObserver.html#observer-targer-options" class="sidebar-link" data-v-40a3448a>observer(targer[, options])</a></li><li class="level-3" data-v-40a3448a><a href="/blog/%E7%AC%94%E8%AE%B0/MutationObserver.html#takerecords" class="sidebar-link" data-v-40a3448a>takeRecords()</a></li><li class="level-3" data-v-40a3448a><a href="/blog/%E7%AC%94%E8%AE%B0/MutationObserver.html#disconnect" class="sidebar-link" data-v-40a3448a>disconnect()</a></li><li class="level-2" data-v-40a3448a><a href="/blog/%E7%AC%94%E8%AE%B0/MutationObserver.html#示例" class="sidebar-link" data-v-40a3448a>示例</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-44bd5a18 data-v-44bd5a18><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-44bd5a18><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-44bd5a18></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-44bd5a18></path></svg></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      欢迎来到 夏栀璃鸢浅汐与潮
    </div> <div class="operation" style="right:90px;bottom:40px;display:none;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:none;" data-v-5775ee02></i></div> <canvas id="banniang" width="150" height="220" class="live2d" style="position:fixed;left:0px;bottom:0px;opacity:0.9;z-index:99999;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.3219c992.js" defer></script><script src="/assets/js/3.95fbbe62.js" defer></script><script src="/assets/js/1.87858ac8.js" defer></script><script src="/assets/js/10.4bcbd4ad.js" defer></script>
  </body>
</html>
